//
//  CHKeyBoardView.swift
//  jyzx
//
//  Created by cyptt on 2022/5/3.
//

import UIKit

open class CHKeyBoardView: UIView {

    var click_y:CGFloat?
   public override func awakeFromNib(){
        super.awakeFromNib()
        self.setUp()
    }
   public override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUp()
    }
    
   public required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   public override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        self.click_y = point.y
        return true
    }
    func setUp(){

        NotificationCenter.default.addObserver(self, selector:#selector(keyboardChangeFrame(noti: )), name:UIResponder.keyboardWillShowNotification , object: nil)
        NotificationCenter.default.addObserver(self, selector:#selector(keyboardHide(noti: )), name:UIResponder.keyboardWillHideNotification , object: nil)
    }
    
    deinit{
        NotificationCenter.default.removeObserver(UIResponder.keyboardWillShowNotification)
        NotificationCenter.default.removeObserver(UIResponder.keyboardWillHideNotification)
    }
    @objc func keyboardChangeFrame(noti:NSNotification){
        print(noti)
        let useInfo:[AnyHashable:Any]? = noti.userInfo
        let value:NSValue? = useInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue

       //键盘高度
//        let height:CGFloat? = value?.cgRectValue.size.height


        let key_y:CGFloat? = value?.cgRectValue.origin.y;
    
       if (self.click_y! > key_y! - 20) {

           //键盘遮挡
           let  duration:CGFloat = useInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as! CGFloat;
           UIView.animate(withDuration: duration) {
               self.transform =
               CGAffineTransform.init(translationX: 0, y: key_y! - self.click_y! - 40)
            
           }
         
       }
    }
    
    @objc func keyboardHide(noti:NSNotification){
        UIView.animate(withDuration: 0.25) {
            self.transform =
            CGAffineTransform.init(translationX: 0, y: 0)
        }
    }
    
    
}

